package com.mku.parkingmanagement_service.service.impl;

import cn.dev33.satoken.util.SaFoxUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mku.parkingmanagement_service.entity.LoginInfo;
import com.mku.parkingmanagement_service.entity.User;
import com.mku.parkingmanagement_service.mapper.LoginInfoMapper;
import com.mku.parkingmanagement_service.mapper.UserMapper;
import com.mku.parkingmanagement_service.service.UserService;
import com.mku.parkingmanagement_service.utils.MyUtils;
import com.mku.parkingmanagement_service.vo.UserVo;
import com.mku.parkingmanagement_service.vo.comm.ResultVO;
import eu.bitwalker.useragentutils.UserAgent;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @className: UserServiceImpl
 * @author: Maoxx
 * @date: 2024/11/8 10:41
 * @Version: 1.0
 * @description:
 */

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private LoginInfoMapper loginInfoMapper;

    @Override
    public ResultVO<Object> login(User user) {
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.eq("username", user.getUsername()).eq("password", user.getPassword());
        User one = userMapper.selectOne(qw);
        if (one != null) {
            if (user.getPassword().equals(one.getPassword())) {
                recommendLoginInfo(one.getUsername(), MyUtils.getRequest()); // 记录登录日志
                return ResultVO.success(one);
            } else {
                return new ResultVO<>().message("密码错误").flag(false);
            }
        }
        return new ResultVO<>().message("用户不存在").flag(false);
    }

    @Override
    public ResultVO<Object> register(User user) {
        user.setCreateTime(LocalDateTime.now());
        return userMapper.insert(user) == 1 ?
                new ResultVO<>()
                        .flag(true)
                        .message("注册成功！")
                : new ResultVO<>()
                .message("注册失败")
                .flag(false);
    }

    @Override
    public Object getUsersPage(UserVo userVo) {
        Page<User> page = new Page<>(userVo.getPagenum(), userVo.getPageSize());
        QueryWrapper<User> qw = new QueryWrapper<>();

        if (!SaFoxUtil.isEmpty(userVo.getUsername())) {
            qw.like("username", userVo.getUsername());
        }

        if (!SaFoxUtil.isEmpty(userVo.getNike())) {
            qw.like("nike", userVo.getNike());
        }

        if (!SaFoxUtil.isEmpty(userVo.getCard())) {
            qw.like("card", userVo.getCard());
        }

        return userMapper.selectPage(page, qw);
    }

    @Override
    public ResultVO<Object> rePassword(Integer uid, String password) {
        if (SaFoxUtil.isEmpty(uid) || SaFoxUtil.isEmpty(password)) {
            return new ResultVO<>().message("参数不能为空").flag(false);
        }
        User user = userMapper.selectById(uid);
        user.setPassword(password);
        int update = userMapper.updateById(user);
        if (update < 1) {
            return ResultVO.fail("修改密码失败！！", null);
        }
        return new ResultVO<>().message("修改密码成功").flag(true);
    }

    @Override
    public List<Object> getUserDataType(String person) {
        return userMapper.getUserDataType(person);
    }

    @Override
    public List<Object> getUserDataMoney(String person) {
        return userMapper.getUserDataMoney(person);
    }

    @Override
    public Object userPay(User user) {
        User user1 = userMapper.selectById(user.getUid());
        if (!SaFoxUtil.isEmpty(user1.getMoney())) {
            user.setMoney(user1.getMoney() + user.getMoney());
        }
        return userMapper.updateById(user) > 0;
    }

    /**
     * 记录登录日志
     */
    private void recommendLoginInfo(String username, HttpServletRequest request) {
        // 创建登录日志信息对象
        LoginInfo loginInfo = new LoginInfo();

        // 获取客户端IP
        String ip = MyUtils.getIpAddress(request);
        // 获取客户端操作信息,包括浏览器类型,操作系统等
        UserAgent userAgent = UserAgent.parseUserAgentString(request.getHeader("User-Agent"));

        // 设置信息
        loginInfo.setIp(ip);
        loginInfo.setLoginTime(LocalDateTime.now());
        loginInfo.setBrowser(userAgent.getBrowser().getName());
        loginInfo.setOs(userAgent.getOperatingSystem().getName());
        loginInfo.setPerson(username);

        // 插入数据库
        loginInfoMapper.insert(loginInfo);
    }


}
